package com.example.shushiyunsst.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.shushiyunsst.R;

public class TransactionBtn extends androidx.appcompat.widget.AppCompatImageButton {

    public static final int AN_JIU_STATUS = 0;

    public static final int HONG_SE_STATUS = 1;

    public static final int LV_SE_STATUS = 2;

    private int ClickCount = 0;

    public TransactionBtn(@NonNull Context context) {
        this(context, null);
    }

    public TransactionBtn(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TransactionBtn(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.TRANSPARENT);
        setImg();

    }

    @Override
    public boolean performClick() {
        ClickCount++;
        setImg();
        return super.performClick();
    }

    public int getStatus() {
        return ClickCount % 3;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            ClickCount++;
            setImg();
        }
        return super.onTouchEvent(event);
    }

    private void setImg() {
        if (getStatus() == HONG_SE_STATUS) {
            int hong_se = R.drawable.ic_trans_red;
            setImageResource(hong_se);
        } else if (getStatus() == AN_JIU_STATUS) {
            int an_jiu = R.drawable.ic_trans_red_green;
            setImageResource(an_jiu);
        } else if (getStatus() == LV_SE_STATUS) {
            int lv_se = R.drawable.ic_trans_green;
            setImageResource(lv_se);
        }
    }
}
